package com.zhu.controller.admin;

import com.zhu.base.http.RestResponse;
import com.zhu.domain.Warehouse;
import com.zhu.model.EchartsVM;
import com.zhu.model.warehouse.WarehouseInsertVM;
import com.zhu.model.warehouse.WarehouseSelectVM;
import com.zhu.model.warehouse.WarehouseUpdateVM;
import com.zhu.service.InventoryRecordService;
import com.zhu.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author JoyBoyZH
 * @description 仓库管理接口
 * @date 2022/6/3
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/gim/api/admin/manageWarehouse")
@Api(tags = "管理员仓库模块说明")
public class AdminWarehouseController {
    private final ModelMapper modelMapper;

    private final WarehouseService warehouseService;

    private final InventoryRecordService inventoryRecordService;


    @Autowired
    public AdminWarehouseController(ModelMapper modelMapper, WarehouseService warehouseService,InventoryRecordService inventoryRecordService) {
        this.modelMapper = modelMapper;
        this.warehouseService = warehouseService;
        this.inventoryRecordService = inventoryRecordService;
    }

    @GetMapping(value = "/selectWarehouse/allWarehouse")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取所有仓库接口说明")
    public RestResponse<Object> selectAllWarehouse() {
        List<Warehouse> warehouseList = warehouseService.getAllWarehouses();
        if (warehouseList.isEmpty()) {
            return RestResponse.failure("没有符合条件的仓库");
        } else {
            return RestResponse.ok(warehouseList);
        }
    }

    @GetMapping(value = "/selectWarehouse/inventoryNum")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取仓库库存分布接口说明")
    public RestResponse<Object> selectWarehouseInventoryNum() {
        List<Warehouse> warehouseList = warehouseService.getAllWarehouses();
        if (warehouseList.isEmpty()) {
            return RestResponse.failure("没有符合条件的仓库");
        } else {
            List<EchartsVM> list = new ArrayList<>();
            for(Warehouse warehouse : warehouseList){
                EchartsVM model = new EchartsVM();
                model.setName(warehouse.getWarehouseName());
                model.setValue(warehouse.getTotalInventory());
                list.add(model);
            }
            return RestResponse.ok(list);
        }
    }


    @PostMapping(value = "/selectWarehouse/condition")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("条件查询仓库接口说明")
    public RestResponse<Object> selectWarehouseByCondition(@RequestBody WarehouseSelectVM model) {
        List<Warehouse> warehouseList = warehouseService.getByDim(model);
        if (warehouseList.isEmpty()) {
            return RestResponse.failure("没有符合条件的仓库");
        } else {
            return RestResponse.ok(warehouseList);
        }
    }


    @PostMapping(value = "/insertWarehouse")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("添加仓库接口说明")
    public RestResponse<Object> insertWarehouse(@RequestBody WarehouseInsertVM model) {
        Warehouse warehouseInfo = warehouseService.getByName(model.getWarehouseName());
        if (warehouseInfo != null) {
            return RestResponse.failure("信息重复");
        } else {
            Warehouse warehouse = new Warehouse();
            warehouse.setWarehouseName(model.getWarehouseName());
            warehouse.setTotalInventory(0);
            warehouse.setCapacity(model.getCapacity());
            warehouseService.saveWarehouse(warehouse);
            return RestResponse.ok("添加成功");
        }
    }

    @GetMapping(value = "/deleteWarehouse/{warehouseId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("删除仓库接口说明")
    public RestResponse<Object> deleteWarehouse(@PathVariable Integer warehouseId) {
        if (warehouseService.deleteWarehouse(warehouseId)) {
            return RestResponse.ok("删除成功");
        } else {
            return RestResponse.failure("删除失败");
        }
    }

    @PostMapping(value = "/updateWarehouse/{warehouseId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改仓库接口说明")
    public RestResponse<Object> updateWarehouse(@PathVariable Integer warehouseId, @RequestBody WarehouseUpdateVM model) {
        Warehouse warehouse = warehouseService.getByWarehouseId(warehouseId);
        if (warehouse.getTotalInventory() > model.getCapacity()) {
            return RestResponse.failure("容量小于当前库存！");
        }
        if(!Objects.equals(warehouse.getWarehouseName(), model.getWarehouseName())){
            inventoryRecordService.updateWarehouseName(warehouse.getWarehouseName(), model.getWarehouseName());
        }
        warehouse.setWarehouseName(model.getWarehouseName());
        warehouse.setCapacity(model.getCapacity());
        if (warehouseService.updateWarehouse(warehouse)) {
            return RestResponse.ok("修改成功");
        } else {
            return RestResponse.failure("修改失败");
        }
    }
}
